# coding=utf-8
import random
from copy import copy

from lib.Character import ProfessionType, RaceType
from lib.Character.Enemy import Enemy
from lib.Lang.main import NO_SKILL
from lib.Skill import EffectType
from lib.Skill.Effect import Effect
from lib.Skill.Skill import Skill
from lib.global_game import player

base_data = {
    'stamina': 5,
    'strengt': 10,
    'agility': 3,
    'intellect': 2,
    'dodge': 0,
    'combo': 0,
    'critical': 0,
    'counter': 0
}
uplevel_random_data = {
    'stamina': 0,
    'strengt': random.randint(0, 3),
    'agility': random.randint(0, 3),
    'intellect': 0,
    'dodge': 0,
    'combo': 0,
    'critical': 0,
    'counter': 0
}


class Battlefield(object):
    def __init__(self):
        super(Battlefield, self).__init__()
        self.timeTurn = 0
        self.overTimeTurn = 1000

        self.exp = 10000
        self.heros = copy(player.heros)
        e1_s1_skill = [
            Effect('e1_s1_e1', EffectType.DAMAGE),
            Effect('e1_s1_e3', EffectType.PHYSICAL_DAMAGE)
        ]
        e1_s2_skill = [
            Effect('e2_s1_e1', EffectType.PHYSICAL_DAMAGE),
            Effect('e2_s1_e3', EffectType.PHYSICAL_DAMAGE)
        ]
        e1_skill = [Skill('e1_s1', 0, e1_s1_skill)]
        e2_skill = [Skill('e2_s1', 0, e1_s2_skill)]
        drops = [
            {'id': 0, 'point': 1},
            {'id': 0, 'point': 30}
        ]
        self.enemys = [
            Enemy('e1', ProfessionType.WARRIOR, RaceType.ANIMAL, base_data, uplevel_random_data, 10, e1_skill, drops),
            Enemy('e2', ProfessionType.WARRIOR, RaceType.INSECT, base_data, uplevel_random_data, 10, e2_skill, drops)
        ]
        self.turn_list = self.heros + self.enemys

    def run(self):
        for char in self.turn_list:
            char.turn_tick += char.speed
            self.overGame()
            has_vertigo = char.has_vertigo_dot()
            if has_vertigo:
                print('{0} 眩晕 跳过回合'.format(char.name))
            if char.turn_tick >= self.overTimeTurn and not has_vertigo and not char.is_dead(False):
                char.effect_buff()
                char.effect_dot()
                self.isWin(self.enemys, self.heros)
                self.overGame()
                if char.is_dead(False):
                    break
                print(char.name + '行动')

                skill = target_enemys = target_friends = None
                if char in self.heros:
                    # raw_input()
                    skills = char.turn_skills()
                    skill = char.select_skill(skills)
                    target_enemys = random.sample(self.enemys, 1)
                    target_friends = random.sample(self.heros, 1)
                if char in self.enemys:
                    skills = char.turn_skills()
                    skill = char.select_skill(skills)
                    target_enemys = random.sample(self.heros, 1)
                    target_friends = random.sample(self.enemys, 1)
                if skill and not char.is_dead(False):
                    char.turn_skill(target_enemys, target_friends, skill)
                else:
                    print(NO_SKILL.format(char.name))
                self.isWin(target_enemys, target_friends)
                self.overGame()
                char.turn_tick = self.timeTurn

    def overGame(self):
        if len(self.heros) <= 0:
            print('敌人 胜利')
            exit()
        if len(self.enemys) <= 0:
            print('玩家 胜利')
            print('获得 {0} 经验'.format(self.exp))
            player.property.expt += self.exp
            exit()

    def isWin(self, target_enemys, target_friends):
        for enemy_char in target_enemys:
            if enemy_char.is_dead():
                if enemy_char in self.enemys:
                    self.enemys.remove(enemy_char)
                if enemy_char in self.heros:
                    self.heros.remove(enemy_char)
                if enemy_char in self.turn_list:
                    self.turn_list.remove(enemy_char)

        for friend_char in target_friends:
            if friend_char.is_dead():
                if friend_char in self.enemys:
                    self.enemys.remove(friend_char)
                if friend_char in self.heros:
                    self.heros.remove(friend_char)
                if friend_char in self.turn_list:
                    self.turn_list.remove(friend_char)
